package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

/**
 * 控制器类 这个级别的类负责和用户打交道,并将请求交给业务层处理,并结合处理结果返回给用户
 */
@Component
public class UserController {
    @Autowired
    private IUserMapper IuserMapper;

    public void start() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入编号:");
        System.out.println("编号1:开始注册");
        System.out.println("编号2:用户登录");
        System.out.println("编号3:用户列表");
        System.out.println("编号4:修改用户信息");
        int num = scanner.nextInt();
        switch (num) {
            case 1:
                userReg();
                break;
            case 2:
                userLogin();
                break;
            case 3:
                userList();
                break;
            case 4:
                userUpdate();
                break;
            default:
                System.out.println("输入非法数字");
        }
    }
    private void userUpdate(){
        Scanner scanner = new Scanner(System.in);
        User user = new User();
        System.out.println("请输入用户名:");
        user.setUsername(scanner.nextLine());
        System.out.println("请输入新密码:");
        user.setPassword(scanner.nextLine());
        System.out.println("请输入新昵称:");
        user.setNickname(scanner.nextLine());
        System.out.println("请输入新年龄:");
        user.setAge(scanner.nextInt());
        int result = IuserMapper.update(user);
        System.out.println(result>0?"更改成功!":"用户名"+user.getUsername()+"不存在!");
    }
    private void userReg() {
        System.out.println("欢迎注册");
        Scanner scanner = new Scanner(System.in);
        User user = new User();
        System.out.println("请输入用户名:");
        user.setUsername(scanner.nextLine());
        System.out.println("请输入密码:");
        user.setPassword(scanner.nextLine());
        System.out.println("请输入昵称:");
        user.setNickname(scanner.nextLine());
        System.out.println("请输入年龄:");
        user.setAge(scanner.nextInt());
        if (IuserMapper.selectByUsername(user.getUsername())==null) {
            int result = IuserMapper.insert(user);
            System.out.println(result > 0 ? "注册成功" : "注册失败");
        }else {
            System.out.println("用户名"+user.getUsername()+"已存在");
        }

    }

    private void userLogin() {
        System.out.println("欢迎登录");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username= scanner.nextLine();
        System.out.println("请输入密码:");
        String password= scanner.nextLine();
        User user = IuserMapper.selectByUsername(username);
            if (user != null){
                if (user.getPassword().equals(password)){
                    System.out.println("登陆成功!欢迎"+user.getNickname()+"回来!");
                }else {
                    System.out.println("登录失败!密码错误");
                }
            }else {
                System.out.println("用户" + username + "不存在!");
            }
    }

    private void userList() {
        List<User> usersList = IuserMapper.selectAll();
            for (User user:usersList){
                System.out.println(user);
            }
        }
    }



